<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" inline label-width="100px" @submit.native.prevent>
      <el-form-item label="单位名称" prop="departmentId">
        <el-cascader v-model="pram.departmentId" :disabled="pram.departmentId ===0" :options="parentOptions" :props="categoryProps" style="width:100%" />
        <!-- allTreeList or parentOptions-->
      </el-form-item>
      <el-form-item label="职位简称" prop="positionDesc">
        <el-input v-model="pram.positionDesc" maxlength="255" placeholder="简称"/>
      </el-form-item>
      <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click.native="handlerOpenInfoAdd(0)" class="position-btn">新增</el-button>

      <el-table :data="pram.positionList" size="mini" :header-cell-style=" {fontWeight:'bold'}" border>
        <!--        <template slot="empty"><span>暂无数据</span></template>-->
        <el-table-column label="单位名称" prop="depName" min-width="200" align="center"/>
        <el-table-column label="职位名称" prop="position" min-width="100" align="center"/>
        <el-table-column label="是否主职" prop="isMajorText" min-width="100" align="center"/>
        <el-table-column label="操作" width="100"  fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="handlerInfoDel(scope.$index,scope.row)" v-hasPermi="['admin:person:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-form-item class="btn-box">
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:person:update','admin:person:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>

    <!-- 职务详情 -->
    <el-dialog
      :visible.sync="positionInfoDialogConfig.visible"
      :title="positionInfoDialogConfig.isCreate === 0? '新增职位':'新增职位'"
      destroy-on-close
      :close-on-click-modal="false"
      :append-to-body="true"
      width="700px"
    >
      <info
        v-if="positionInfoDialogConfig.visible"
        :is-create="positionInfoDialogConfig.isCreate"
        :edit-data="positionInfoDialogConfig.editData"
        :edit-key="positionInfoDialogConfig.editKey"
        :all-parent-options="parentOptions"
        @hideInfoDialog="hideInfoDialog"
        @assignInfoSaveAndHideInfoDialog="assignInfoSaveAndHideInfoDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import {Debounce} from '@/utils/validate'
import info from './info'
export default {
  // name: "edit"
  components: { info },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allTreeList: {
      type: Array
    }
  },
  data() {
    return {
      constants:this.$constants,
      pram: {
        departmentId:null,
        positionDesc: null,
        positionList: [],

        //编辑额外
        removeId:'',
        addArr:[],
      },
      removeTempId:'',

      rules: {
        departmentId: [{ required: true, message: '请选择单位', trigger: ['blur', 'change'] }],
        positionDesc: [{ required: true, message: '请填简称', trigger: ['blur', 'change'] }],
      },
      positionInfoDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {},
        editKey: -1,
      },
      //treeList:[],

      parentOptions: [],
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'child',
        expandTrigger: 'hover',
        checkStrictly: true,
        emitPath: false
      },
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hidePositionDialog')
    },

    initEditData() {
      this.parentOptions = [...this.allTreeList]
      this.addTreeListLabelForCasCard(this.parentOptions, 'child')
      //this.treeList = this.allTreeList

      //if (this.isCreate !== 1) return //编辑
      const { departmentId, positionDesc, positionList} = JSON.parse(JSON.stringify(this.editData)) //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.departmentId = departmentId
      this.pram.positionDesc = positionDesc
      this.pram.positionList = positionList||[]
    },
    addTreeListLabelForCasCard(arr, child) {
      arr.forEach((o,i) => {
        if (o.child && o.child.length) {
          o.disabled = true
          o.child.forEach((j) => {
            //j.disabled = true
          })
        }
      })
    },

    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      this.$emit('assignPositionSaveAndHidePositionDialog',this.pram)
    },
    handlerEdit() {
      this.pram.addArr=this.pram.positionList.filter(item=>!item.id)
      this.pram.removeId = this.removeTempId
      this.$emit('assignPositionEditAndHidePositionDialog',this.pram)
    },

    // 职位新增
    handlerOpenInfoAdd(isCreate, editDate, editKey) {
      this.positionInfoDialogConfig.editData = editDate
      this.positionInfoDialogConfig.isCreate = isCreate
      this.positionInfoDialogConfig.visible = true
      this.positionInfoDialogConfig.editKey = editKey
    },
    hideInfoDialog() {
      this.positionInfoDialogConfig.visible = false
    },
    assignInfoSaveAndHideInfoDialog(data) {
      setTimeout(() => {
        this.pram.positionList.push(data);
        this.positionInfoDialogConfig.visible = false
      }, 200);
    },
    handlerInfoDel(key,row){
      //删除单行简历数据
      const  _xKey = key
      this.pram.positionList.splice(_xKey,1);//index 当前元素索引；1：需要删除的元素个数
      //data=data.filter(item=>item.name!='张三');

      if (this.isCreate === 1 && row.id) {
        //编辑时的删除，记录删除ID
        const delId = row.id
        this.removeTempId+=delId+','
      }
    },

  }
}
</script>

<style scoped lang="scss">
  .position-btn{
    margin-top:3px;
    float:right;
  }
  .btn-box{
    margin-top:20px;
  }
  .dislabel{
    ::v-deep.el-form-item__label{
      width:100px!important;
    }
    ::v-deep.el-form-item__content{
      margin-left:100px!important;
    }
    ::v-deep.el-input--small .el-input__inner,
    ::v-deep.el-input--medium .el-input__inner{
      height:36px;
      line-height:36px;
      font-size:12px;
    }
    ::v-deep.el-textarea__inner{
      font-size:12px;
    }

  }
</style>
